class ProductsController < ApplicationController
 layout "background",
        :only=>[:product_edit,:product_create,:product_destroy,
                :product_list,:product_new,:product_show,:product_update]
  
before_filter:authorize,
               :only=>[:product_edit,:product_create,:product_destroy,
                :product_list,:product_new,:product_show,:product_update,:product_offsell]
              
before_filter:has_shop,
               :only=>[:product_edit,:product_create,:product_destroy,
                :product_list,:product_new,:product_show,:product_update,:product_offsell]
              
before_filter:check_product_owner,
               :only=>[:product_edit,:product_destroy,:product_show,:product_update,:product_offsell]
    
before_filter:post?,
               :only=>[:product_create,:product_update,:product_offsell,:product_destroy]
  
  def authorize
    unless User.find_by_id(session[:user_id])
      notice("请先登录！")
      session[:last_uri]=request.request_uri
      redirect_to_login
    end
  end
  
  def has_shop
    begin
      @shop=Shop.find_by_user_id(session[:user_id])
    rescue ActiveRecord::RecordNotFound
      redirect_to(:controller=>:shop,:action=>:shop_new)
    end
  end
  
  def check_product_owner
    begin
      @product=Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      notice("商品不存在！")
      redirect_to(:controller=>"manage",:action=>:error_msg)
    else
      unless @product.shop.user.id==session[:user_id]
        notice("你没有权限操作该产品！")
        redirect_to(:controller=>"manage",:action=>:error_msg)
      end
    end
  end
  
  def post?
    unless request.post?
      notice("非正常操作！")
      redirect_to(:controller=>:manage,:action=>:error_msg)
    end
  end
  
  def index
    @products = Product.find(:all)
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])
    @comments=Productcomment.find(:all,:conditions=>["product_id=?",params[:id]])
    render(:layout=>"layouts/common")
  end
  
   def add_to_cart
    if request.post?
      begin 
        product =Product.find(params[:id])
      rescue ActiveRecord::RecordNotFound
        logger.error("Attempt to access invalid product #{params[:id]}")
      else        
        @cart= find_cart
        if @cart.shop_id.nil?
          @cart.shop_id=product.shop.id
        end
        if @cart.shop_id==product.shop.id
          @cart.add_product(product)
        else
          notice("购物车只能放同一商铺的产品")
        end
      end
    end 
   
    redirect_to(:controller=>"shopping_cart",:action=>"index")     
  end
  
   def delete_from_cart
     if request.post?
       begin
         product =Product.find(params[:id])
       rescue ActiveRecord::RecordNotFound
         logger.error("session 内找不到id为#{params[:id]}的产品")
         redirect_to(:controller=>"shopping_cart")
       else
         @cart=find_cart
         @cart.delete_product_at(product)
       end
     
     end
   end
  
  def empty_cart
       session[:cart]=nil
       redirect_to_index("购物车已被清空！")
   end
 
  def product_show
  end

  def product_list
    @products=Product.paginate(:all,:conditions=>["shop_id=?",@shop.id],:page => params[:page]||1,:per_page => 5)
    
  end

  def product_new
  end
  
  def product_create
      @product = Product.new(params[:product])
      @product.shop=@shop
      @product.status="onsell"
      begin
        @product.save!
      rescue ActiveRecord::RecordNotSaved
        logger.error("product #{@product.title} created error")
        redirect_to_error_msg("创建失败！")
      else
        notice("创建成功！")
        redirect_to(:action=>:product_show,:id=>@product.id)
      end    
  end
  
  def product_edit
  end
  
  def product_update
    begin 
      @product.update_attributes!(params[:product])
    rescue ActiveRecord::RecordNotSaved
      logger.error("product #{@product.title} updated error")
      redirect_to_error_msg("更新失败!")
    else
      notice("更新成功！")
      redirect_to(:action=>:product_show,:id=>@product.id)
    end     
  end
  
  def product_offsell
       if @product.update_attribute("status", "offsell")
         notice("撤下成功！")
       else 
         notice("撤下失败！")        
       end
       redirect_to(:action=>:product_list)
  end
  
  def product_destroy
    begin
    @product.destroy
    rescue ActiveRecord::ActiveRecordError
      logger.error("product #{@product.title} deleted error")
      redirect_to_error_msg("撤下商品失败!")
    else
      notice("撤下商品成功！")
      redirect_to(:action=>:product_list)
    end 
  end
  
  private   
  def find_cart
      session[:cart] ||=Cart.new
  end
  
end
